package com.xiashitech.authservice.config;

import com.tencent.bk.sdk.iam.config.IamConfiguration;
import com.tencent.bk.sdk.iam.helper.AuthHelper;
import com.tencent.bk.sdk.iam.service.HttpClientService;
import com.tencent.bk.sdk.iam.service.PolicyService;
import com.tencent.bk.sdk.iam.service.TokenService;
import com.tencent.bk.sdk.iam.service.impl.DefaultHttpClientServiceImpl;
import com.tencent.bk.sdk.iam.service.impl.PolicyServiceImpl;
import com.tencent.bk.sdk.iam.service.impl.TokenServiceImpl;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

public class AuthHelperCache {
    private static Map<String, AuthHelper> cacheAuthHelpers = new HashMap();

    public static Map<String, AuthHelper> getCacheAuthHelpers() {
        return cacheAuthHelpers;
    }

    public static void initialAuthHelper(String systemId, String appCode, String appSecret, String iamBaseUrl) {
        if (cacheAuthHelpers.containsKey(systemId)) {
            return;
        }
        IamConfiguration iamConfiguration = new IamConfiguration(systemId, appCode, appSecret, iamBaseUrl);
        HttpClientService httpClientService = new DefaultHttpClientServiceImpl(iamConfiguration);
        TokenService tokenService = new TokenServiceImpl(iamConfiguration, httpClientService);
        PolicyService policyService = new PolicyServiceImpl(iamConfiguration, httpClientService);
        AuthHelper authHelper = new AuthHelper(tokenService, policyService, iamConfiguration);
        cacheAuthHelpers.put(systemId, authHelper);
    }
}
